﻿Imports MySql.Data.MySqlClient
Imports System.IO

Public Class ventas

    Private Sub ventas_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.Show()
    End Sub
    Dim connectionstring As String
    Dim commandtext As String
    Dim commandtext2 As String
    Dim adapter As MySqlDataAdapter
    Dim adapter2 As MySqlDataAdapter
    Dim table As DataTable
    Dim table2 As DataTable

    Private Sub ventas_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        connectionstring = "server=localhost;user id=root;password=root;database=zoo;"
        commandtext = "Select * from producto"
        commandtext2 = "Select * from carrito"
        Try

            adapter = New MySqlDataAdapter(commandtext, connectionstring)
            adapter2 = New MySqlDataAdapter(commandtext2, connectionstring)
            table = New DataTable
            table2 = New DataTable
            adapter.Fill(table)
            dgv_ventas.DataSource = table

            txt_id.DataBindings.Add("Text", table, "id")
            txt_nombre.DataBindings.Add("Text", table, "nombre")
            txt_categoria.DataBindings.Add("Text", table, "categoria")
            txt_stock.DataBindings.Add("Text", table, "stock")
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally

        End Try
        commandtext2 = "Select * from carrito"
        Try

            adapter2 = New MySqlDataAdapter(commandtext2, connectionstring)
            table2 = New DataTable
            adapter2.Fill(table2)
            DataGridView1.DataSource = table2
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally

        End Try
        registro()
    End Sub

    Private Sub btn_siguiente_Click(sender As System.Object, e As System.EventArgs) Handles btn_siguiente.Click
        BindingContext(table).Position += 1
        registro()
    End Sub

    Private Sub btn_inicio_Click(sender As System.Object, e As System.EventArgs) Handles btn_inicio.Click
        BindingContext(table).Position = 0
        registro()
    End Sub

    Private Sub btn_anterior_Click(sender As System.Object, e As System.EventArgs) Handles btn_anterior.Click
        BindingContext(table).Position -= 1
        registro()
    End Sub

    Private Sub btn_ultimo_Click(sender As System.Object, e As System.EventArgs) Handles btn_ultimo.Click
        BindingContext(table).Position = BindingContext(table).Count - 1
        registro()
    End Sub

    Private Sub btn_aniadir_Click(sender As System.Object, e As System.EventArgs) Handles btn_aniadir.Click
        connectionstring = "server=localhost;user id=root;password=root;database=zoo;"
        Dim connection As New MySqlConnection(connectionstring)
        connection.Open()
        Dim StrVar As String = Nothing
        Dim rd, rd2 As MySqlDataReader
        Dim cmd, cmd2 As New MySqlCommand
        commandtext2 = "Select * from carrito"
        adapter2 = New MySqlDataAdapter(commandtext2, connectionstring)


        cmd2.CommandText = "Select stock from producto where  id= @id"
        cmd2.Parameters.AddWithValue("id", txt_id.Text)
        cmd2.Connection = connection
        rd2 = cmd2.ExecuteReader
        If rd2.Read Then
            StrVar = rd2.GetString(0)
            rd2.Close()
            If StrVar = "=" Then

                MsgBox("No quedan existencias")



            Else


                cmd.CommandText = "Select * from carrito where  id= @id"
                cmd.Parameters.AddWithValue("id", txt_id.Text)
                cmd.Connection = connection
                rd = cmd.ExecuteReader
                If rd.Read Then
                    StrVar = rd.GetString(0)
                End If
                rd.Close()
                connection.Close()
                If StrVar = txt_id.Text Then
                    Using sqlCommand As New MySqlCommand()
                        With sqlCommand
                            .CommandText = "update carrito set cantidad=cantidad+1 where id=@id"
                            .Connection = connection
                            .CommandType = CommandType.Text
                            .Parameters.AddWithValue("@id", txt_id.Text)
                        End With
                        Try
                            connection.Open()
                            sqlCommand.ExecuteNonQuery()
                        Catch ex As MySqlException
                            Dim fs As New FileStream(".\logzoo.txt", FileMode.Append, FileAccess.Write)
                            Dim sw As New StreamWriter(fs)
                            sw.WriteLine(ex.Message.ToString)
                            sw.Close()
                            fs.Close()

                        Finally
                            connection.Close()
                            table2.Clear()
                            adapter2.Fill(table2)
                            DataGridView1.DataSource = table2
                        End Try
                    End Using
                Else
                    Using sqlCommand As New MySqlCommand()
                        With sqlCommand
                            .CommandText = "INSERT INTO carrito (id,nombre, cantidad) values (@id,@nombre, @cantidad)"
                            .Connection = connection
                            .CommandType = CommandType.Text
                            .Parameters.AddWithValue("@id", txt_id.Text)
                            .Parameters.AddWithValue("@nombre", txt_nombre.Text)
                            .Parameters.AddWithValue("@cantidad", "1")
                        End With
                        Try
                            connection.Open()
                            sqlCommand.ExecuteNonQuery()
                        Catch ex As MySqlException
                            Dim fs As New FileStream(".\logzoo.txt", FileMode.Append, FileAccess.Write)
                            Dim sw As New StreamWriter(fs)
                            sw.WriteLine(ex.Message.ToString)
                            sw.Close()
                            fs.Close()
                        Finally
                            connection.Close()
                            table2.Clear()
                            adapter2.Fill(table2)
                            DataGridView1.DataSource = table2
                        End Try
                    End Using
                End If
            End If
        End If






    End Sub

    Private Sub CarritoBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs)
        Me.Validate()
        Me.CarritoBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ZooDataSet)

    End Sub


    Private Sub btn_eliminar_Click(sender As System.Object, e As System.EventArgs) Handles btn_eliminar.Click
        connectionstring = "server=localhost;user id=root;password=root;database=zoo;"
        Dim connection As New MySqlConnection(connectionstring)
        'Dim res As Integer
        Dim eliminar As String = "Delete from carrito where id=@id"
        Dim comando As New MySqlCommand(eliminar, connection)

        comando.Parameters.AddWithValue("@id", txt_id.Text)
        'res = MsgBox("Seguro eliminar?" & TextBox3.Text, 20, "Aviso")
        connection.Open()
        comando.ExecuteNonQuery()
        connection.Close()

        table2.Clear()
        adapter2.Fill(table2)

        DataGridView1.DataSource = table2
    End Sub

    Private Sub btn_comprar_Click(sender As System.Object, e As System.EventArgs) Handles btn_comprar.Click
        connectionstring = "server=localhost;user id=root;password=root;database=zoo;"
        Dim connection As New MySqlConnection(connectionstring)
        Dim eliminar As String = "update producto set stock=stock-(select cantidad from carrito where id=(select id from carrito limit 1)) where id =(select id from carrito limit 1);delete from carrito limit 1;"
        Dim comando As New MySqlCommand(eliminar, connection)

        connection.Open()

        Dim count As Integer
        Dim rd As MySqlDataReader
        Dim cmd As New MySqlCommand


        cmd.CommandText = "SELECT COUNT(*) FROM carrito"
        cmd.Connection = connection
        rd = cmd.ExecuteReader
        If rd.Read Then
            count = rd.GetInt32(0)
        End If


        rd.Close()

        For i As Integer = 0 To count

            comando.ExecuteNonQuery()

        Next


        connection.Close()

        commandtext = "Select * from producto"
        adapter = New MySqlDataAdapter(commandtext, connectionstring)
        table.Clear()
        adapter.Fill(table)

        dgv_ventas.DataSource = table

        table2.Clear()
        adapter2.Fill(table2)
        DataGridView1.DataSource = table2
    End Sub

    Private Sub registro()
        lbl_cont.Text = ((Me.BindingContext(table).Position + 1).ToString + " de ") + BindingContext(table).Count.ToString
    End Sub
End Class